package com.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.BaseContext;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.entity.Msg;
import com.sun.entity.User;
import com.sun.service.MsgService;
import com.sun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/4/3 12:58
 * @FileName: MsgController
 * @Description: ""
 */
@RestController
@RequestMapping("/msg")
public class MsgController {
    @Autowired
    private MsgService msgService;
    @Autowired
    private UserService userService;

    @GetMapping("/getMsg/{msgId}")
    public Result<Msg> getMsg(@PathVariable Long msgId) {
        Msg msg = msgService.getById(msgId);
        return Result.success(msg);
    }

    /**
     * 查询我发送的消息
     */
    @GetMapping("/send")
    public Result<List<Msg>> getMySend() {
        LambdaQueryWrapper<Msg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Msg::getMsgUserId, BaseContext.getCurrentId());
        List<Msg> msgList = msgService.list(lambdaQueryWrapper);
        return Result.success(msgList);
    }

    /**
     * 查询我收到的消息
     */
    @GetMapping("/receive")
    public Result<List<Msg>> getMyReceive() {
        LambdaQueryWrapper<Msg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Msg::getUserId, BaseContext.getCurrentId()).or().eq(Msg::getUserId, 11111111);
        List<Msg> msgList = msgService.list(lambdaQueryWrapper);
        return Result.success(msgList);
    }

    /**
     * 分页查询消息(未查看，已查看，所有）
     *
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "limit", defaultValue = "10") int pageSize,
                             Integer status, String title) {

        Page<Msg> pageInfo = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<Msg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(null != status, Msg::getStatus, status);
        lambdaQueryWrapper.like(null != title, Msg::getTitle, title);
        lambdaQueryWrapper.ne(Msg::getTitle, "库存下限报警").ne(Msg::getTitle, "有效期预警").ne(Msg::getTitle, "过期药品");
        msgService.page(pageInfo, lambdaQueryWrapper);

        List<Msg> records = pageInfo.getRecords();
        List<Msg> msgs = records.stream().map(item -> {
            LambdaQueryWrapper<User> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(User::getId, item.getMsgUserId());
            User user = userService.getOne(lambdaQueryWrapper1);
            item.setMsgUserName(user.getNickName());
            return item;
        }).collect(Collectors.toList());

        pageInfo.setRecords(msgs);

        return Result.success(pageInfo);
    }


    /**
     * 新增消息
     */
    @SystemCrmlog(title = "消息管理模块", type = "INSERT", description = "新增通知公告")
    @PostMapping
    public Result<String> add(@RequestBody Msg msg) {
        msg.setMsgUserId(BaseContext.getCurrentId());
        msg.setStatus(0);
        msg.setDateTime(LocalDateTime.now());
        msg.setUserId(11111111L);
        msg.setDrugName("无");
        msgService.save(msg);
        return Result.success("新增成功");
    }

    /**
     * 删除消息
     */
    @SystemCrmlog(title = "消息管理模块", type = "DELETE", description = "删除通知公告")
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        msgService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 修改状态
     */
    @SystemCrmlog(title = "消息管理模块", type = "UPDATE", description = "修改通知公告的状态")
    @PutMapping("/status")
    public Result<String> updateStatus(@RequestBody Msg msg) {
        msg.setStatus(1);
        msgService.updateById(msg);
        return Result.success("修改成功");
    }

    /**
     * 修改状态
     */
    @SystemCrmlog(title = "消息管理模块", type = "UPDATE", description = "修改通知公告的状态")
    @PutMapping("/status2")
    public Result<String> updateStatus2(@RequestBody Msg msg) {
        msg.setStatus(2);
        msgService.updateById(msg);
        return Result.success("修改成功");
    }

    /**
     * 修改消息
     */
    @SystemCrmlog(title = "消息管理模块", type = "UPDATE", description = "修改通知公告")
    @PutMapping
    public Result<String> update(@RequestBody Msg msg) {
        msgService.updateById(msg);
        return Result.success("修改成功");
    }
}
